//package com.itheima.tlias.controller;
//
//import com.google.code.kaptcha.Producer;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.imageio.ImageIO;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.awt.image.BufferedImage;
//import java.io.ByteArrayOutputStream;
//import java.util.Base64;
//
//@RestController
//public class CaptchaController {
//
//    @Autowired
//    private Producer captchaProducer;
//
//    @GetMapping("/captcha")
//    public String getCaptcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        // 1. 生成验证码文本
//        String code = captchaProducer.createText();
//
//        // 2. 生成验证码图片
//        BufferedImage image = captchaProducer.createImage(code);
//
//        // 3. 把验证码存到 session 中，用于后续验证
//        request.getSession().setAttribute("captcha", code);
//
//        // 4.把图片转成 Base64 返回给前端
//        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//        ImageIO.write(image, "png", outputStream);
//        String base64Image = Base64.getEncoder().encodeToString(outputStream.toByteArray());
//
//        return "data:image/png;base64," + base64Image;
//    }
//}